﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace homework8
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        Search s = new Search();
        private async void button1_Click(object sender, EventArgs e)
        {
            string wd = EncodeUrl();
            if (wd != "")
            {
                string url_baidu = "https://www.baidu.com/s?wd=" + wd;
                string url_bing = "https://cn.bing.com/search?q=" + wd;
                richTextBox1.Text = await s.GetSearch(url_baidu);
                richTextBox2.Text = await s.GetSearch(url_bing);
            }
            else
            {
                richTextBox1.Text = "搜索栏不得为空";
            }
        }

        public string EncodeUrl()
        {
            if (textBox1.Text != null)
            {
                byte[] bytes = Encoding.UTF8.GetBytes(textBox1.Text);
                string res = "";
                for (int i = 0; i < bytes.Length; i++)
                {
                    res += "%" + bytes[i].ToString("X2");
                }
                return res;
            }
            else
                return "";
        }
    }
}
